﻿using ELTE.SEM.Data.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ELTE.SEM.Data
{
    public static class SemFolderEx
    {
        public static SemFolderViewModel GetViewModel(this SemFolder folder, SemFolderViewModel parentFolder)
        {
            SemFolderViewModel viewModel = null;
            try
            {
                viewModel = new SemFolderViewModel
                {
                    Id = folder.Id,
                    FolderName = folder.FolderName,
                    FullPath = folder.FullPath,
                };
                if (parentFolder == null)
                {
                    viewModel.Parent = folder.Parent == null ? null : folder.Parent.GetViewModel(null);
                }
                else
                {
                    viewModel.Parent = parentFolder;
                }
                viewModel.Children = folder.GetChildren(viewModel);
            }
            catch (Exception ex)
            {
                
            }
            
            return viewModel;
        }

        public static List<SemFolderViewModel> GetChildren(this SemFolder folder, SemFolderViewModel currentFolderViewModel)
        {
            List<SemFolderViewModel> children = new List<SemFolderViewModel>();
            try
            {
                if (folder.Children != null && folder.Children.Count > 0)
                {
                    foreach (var child in folder.Children)
                    {
                        children.Add(child.GetViewModel(currentFolderViewModel));
                    }
                }
            }
            catch (Exception)
            {
                
            }
            return children;
        }

    }
}
